<script setup>
import { ref, onMounted, computed, watch, nextTick, onUnmounted } from 'vue'
const loadingRef = ref()
const isFinish = ref(false)
const loading = ref(false)
import SvgIcon from '../../components/SvgIcon.vue'
import { useIntersectionObserver } from '@vueuse/core'
const targetIsIntersecting = ref(false)
const emit = defineEmits(['load'])
const props = defineProps({
  isFinish: {
    type: Boolean,
    default: false
  }
});


useIntersectionObserver(loadingRef, ([{ isIntersecting }], observerElement) => {
  // 获取当前交叉状态
  targetIsIntersecting.value = isIntersecting
  // 触发 load
  emitLoad()
})
const emitLoad = () => {
  if (targetIsIntersecting.value) {
    loading.value = true
    emit('load')
  }
}
// watch
</script>
<template>
  <div>
    <slot></slot>
    <div ref="loadingRef" class="h-6 py-4">
      <div v-show="loading" class="flex w-24">
        <SvgIcon name="infinite-load" class="w-4 h-4 mx-auto animate-spin" />
        <span class="text-sm text-gray-500">加载中...</span>
      </div>
      <div v-if="isFinish">没有更多</div>
    </div>
  </div>
</template>
<style lang="scss" scoped></style>
